<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\store\shop;

use app\common\model\store\shop\ClerkGroup as ClerkGroupModel;
use think\facade\Db;

/**
 * 分销商资金明细模型
 * Class Capital
 * @package app\store\model\dealer
 */
class ClerkGroup extends ClerkGroupModel
{
    public function getList(array $param = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);
        // 查询列表记录
        $list = $this
            ->where($filter)
            ->where('is_delete',0)

            ->order(['create_time' => 'desc'])
            ->paginate(15);

            foreach ($list as $key => $value) {
                $clerkList=Db::name('store_shop_clerk')->where('clerk_id','in',$value['clerk_ids'])
                ->field('real_name,clerk_id,mobile')->select()->toArray();
                $clerks=array_column($clerkList, 'real_name');
                $list[$key]['clerks']=implode(',', $clerks);
                $list[$key]['clerkList']=$clerkList;
            }
   
        return $list;
    }
  public function getListAll(array $param = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);
        // 查询列表记录
        $list = $this
            ->where($filter)
            ->where('is_delete',0)

            ->order(['create_time' => 'desc'])
            ->select()->toArray();

   
        return $list;
    }

    /**
     * 设置检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = []): array
    {
        // 默认参数
        $params = $this->setQueryDefaultValue($param, [
            'search'  => '', // 关键词类型 (10订单号 20分销商ID 30会员ID)
            'searchValue' => '', // 关键词内容
            'clerkId'     => -1, // 是否已结算佣金
            'betweenTime' => [], // 起止时间
        ]);
        // 检索查询条件
        $filter = [];

        // 起止时间
        if (! empty($params['betweenTime'])) {
            $times    = between_time($params['betweenTime']);
            $filter[] = ['create_time', '>=', $times['start_time']];
            $filter[] = ['create_time', '<', $times['end_time'] + 86400];
        }

        !empty($params['search']) && $filter[] = ['name', 'like', '%'.$params['search'].'%'];
        $params['clerkId'] > -1 && $filter[] = ['clerk_id', '=', $params['clerkId']];

        return $filter;
    }

     public function add(array $data): bool
    {
       
        $data['store_id'] = self::$storeId;
        return $this->save($data);
    }
 public function setDelete(): bool
    {
        return $this->save(['is_delete' => 1]);
    }

}
